/**
 * 
 */
package games;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 * @author Owner
 *
 */
public class Dictator extends PassNumberGame {
	/**
	 * 
	 */
	private static final long serialVersionUID=1L;
	
	private static final String GAME_NAME="Dictator";
	private static final String GAME_ENDED="gameEnded";
	
	private static final String FIRST_LINE="You are Player 1.";
	private static final String SECOND_LINE="Player 2 must accept what you offer.";
	private static final String THIRD_LINE="You can offer $%d. How much will you offer?";

	/**
	 * @param runner
	 */
	public Dictator(Runner runner) {
		this(runner,0,15);
	}

	public Dictator(Runner runner,int start,int maximum,int player) {
		super(runner,GAME_NAME);
		this.maximum=maximum;
		this.start=start;
		this.player=player;
	}
	
	public Dictator(Runner runner,int start,int maximum) {
		this(runner,start,maximum,1);
	}
	
	public void initComponents() {
		setLayout(new BorderLayout());
	
		number=start;
		str=Integer.toString(number);
		selected=NUMBER;

		main=new JPanel(new BorderLayout());
		header=new JPanel(new BorderLayout());
		headerPainter=new HeaderPainter(FIRST_LINE,SECOND_LINE,
				String.format(THIRD_LINE,maximum));
		header.add(headerPainter);
		main.add(BorderLayout.NORTH,header);
		painter=new NumberPainter();
		
		main.add(BorderLayout.CENTER,painter);
		add(BorderLayout.CENTER,main);
		


		clock=new Timing();
		getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"),"selector");
		getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("G"),"changer");
		
		getActionMap().put("selector",new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID=1L;

			public void actionPerformed(ActionEvent e) {
				//System.err.println("Selector pushed");
				selected=(selected+1)%2;
				repaint();
			}
		});		
		getActionMap().put("changer",new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID=1L;

			public void actionPerformed(ActionEvent e) {
				//System.err.println("Changer pushed");
				if(selected==GO) {
	      	suspend();
	      	time=clock.getElapsedTime();
					log(GAME_ENDED,number);
					finished(GAME_NAME,time);
				}
				else {
					number=(number+1)%(maximum+1);
					str=Integer.toString(number);
					repaint();
				}
			}
		});
		
	  proceed();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame f=new JFrame();
		f.setLayout(new BorderLayout());
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dictator ap=new Dictator(new Runner());
		ap.init();
		ap.start();
		f.add("Center",ap);
		f.pack();
		f.setVisible(true);
		f.setSize(600,500);
	}
}
